﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <!-- <a-button v-if="hasPerm('Cust_CustomerInfoFormal.Add')" type="primary" icon="plus" @click="handleAdd()"
        >客户备案</a-button
      > -->

      <!-- <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button> -->
      <!-- <a-button type="primary" @click="Distribute(selectedRowKeys)" :disabled="!hasSelected()">一键分配</a-button> -->
      <!-- <a-button type="primary" @click="PllAoo()">客户分配</a-button> -->
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <!-- <a-col :md="4" :sm="24">
            <a-form-item>
              <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
            </a-form-item>
          </a-col> -->
          <a-col :md="4" :sm="24" style="display:flex;">
              <a-button type="primary" icon="redo" @click="getDataList()" style="margin-right:10px">刷新</a-button>
            <a-form-item>
              <!-- <a-input
                @pressEnter=" () => {
                  this.pagination.current = 1
                  this.getDataList()
                }"
                v-model="queryParam.cCusName"
                placeholder="客户名称" /> -->
              
              <searchTip
                v-model="queryParam.cCusName"
                style="display: inline-block"
                placeholder="客户名称"
                :list="searchTipResultList"
                @enterKeyDown="getDataList"
                @change="toGetSearchTipList"
                @blur="keywordBlurFun"
                @select="selectSearchTipFun"
              />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询
            </a-button>
            <a-button style="margin-left: 8px" @click="Repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 'calc(100vh - 340px)' }"
      :columns="columns"
      :rowKey="(row) => row.cCusCode"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
      <template slot="AllocationStatus" slot-scope="text, record">
        <a-tag v-if="record.AllocationStatus !== 1" @click="Distribute([record.cCusCode])" color="red">待分配</a-tag>
        <a-tag v-else-if="record.AllocationStatus === 1" @click="Cancel(record)" color="green">已分配</a-tag>
      </template>
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <!-- <a v-if="record.AllocationStatus === 0" @click="Distribute([record.cCusCode])">分配</a>
          <a-divider v-if="record.AllocationStatus === 0" type="vertical" />
          <a v-if="record.AllocationStatus === 1" @click="Cancel(record)">解除</a>
          <a-divider v-if="record.AllocationStatus === 1" type="vertical" /> -->
          <!-- <a @click="handleEdit(record.cCusCode)">详情</a> -->
          <!-- <a-divider type="vertical" /> -->
          <a v-if="hasPerm('Cust_CustomerInfoFormal.Edit')" @click="handleRevise(record)">详情</a>
          <!-- <a-divider
            v-if="hasPerm('Cust_CustomerInfoFormal.Edit') && hasPerm('Cust_CustomerInfoFormal.Delete')"
            type="vertical"
          />
          <a @click="contactPerson(record)">联系人</a> -->
          <!-- <a-divider type="vertical" />

          <a @click="History(record)">分配历史</a> -->
          <!-- <a-divider type="vertical" />
          <a v-if="hasPerm('Cust_CustomerInfoFormal.Delete')" @click="handleDelete([record.cCusCode])">删除</a> -->
        </template>
      </span>
    </a-table>

    <EditForm ref="details" :parentObj="this"></EditForm>
    <Details ref="Detail" :parentObj="this"></Details>
    <PllAOO ref="PllAOO" />
    <MyCustomer ref="MyCustomer" :IsFormal="IsFormal" :parentObj="this"></MyCustomer>

    <List ref="List" />
  </a-card>
</template>

<script>
// 客户分配
import MyCustomer from '../MyCustomer/EditForm.vue'
import EditForm from '../Customer/EditForm'
import Details from '../Customer/EditForm.vue'
import PllAOO from '../Customer/PllAOO.vue'
import List from '../Cust_CustomerLinkMan/List.vue'
import moment from 'moment'
import searchTip from '@/components/components/SearchTip.vue'

const columns = [
  { title: '客户编码', dataIndex: 'cCusCode', width: '8%' },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left' },
  { title: '创建时间', dataIndex: 'CreateTime', width: '10%', scopedSlots: { customRender: 'CreateTime' } },
  { title: '所在省', dataIndex: 'ProvinceName', align: 'left', width: '12%' },
  { title: '所在市', dataIndex: 'CityName', align: 'left', width: '12%' },
  { title: '所在区', dataIndex: 'CountyName', align: 'left', width: '12%' },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '10%' },
]

export default {
  components: {
    EditForm,
    List,
    MyCustomer,
    Details,
    PllAOO,
    searchTip,
  },
  mounted() {
    this.getDataList()
  },
  data() {
    return {
      IsFormal: true,
      data: [],
      list: [], // 省级
      listChildren: [], // 市
      county: [], // 县
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true,
      },
      filters: {},
      sorter: { field: 'cCusCode', order: 'desc' },

      queryParam: { cCusType: 1 },
      loading: false,
      columns,
      selectedRowKeys: [],
      selectedRows: [],
      searchTipResultList: [],
    }
  },
  methods: {
    // 获取省份信息
    getlist(e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=0', {}).then((resJson) => {
        this.loading = false
        this.list = resJson.Data

        // this.getlistUser()
      })
    },
    inputKeyDown(e) {
      if (e.keyCode === 13) {
        if (!this.queryParam.cCusName) return this.$message.error('请输入查询条件')

        this.getDataList()
      }
    },

    PllAoo() {
      this.$refs.PllAOO.openForm()
    },
    handleAdd() {
      this.$refs.Detail.openForm(null, '添加')
    },
    handleRevise(id) {
      this.$refs.Detail.openForm(id, '编辑', null, true)
    },
    // 省市区的模糊查询
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    Cancel(row) {
      var thisObj = this
      this.$confirm('确定取消分配吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http
            .post(
              `/CustomerManagement/Cust_CustomerAllocation/DeleteDataForDissolution?CustomerId=${row.cCusCode}&&IsFormal= true`
            )
            .then((resJson) => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
        })
      })
    },
    Repossess() {
      this.queryParam = { cCusType: 1 }
      this.getDataList()
    },
    CheckboxProps(record) {
      return {
        props: {
          disabled: record.AllocationStatus === 1,
        },
      }
    },

    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    Distribute(row) {
      this.IsFormal = true
      this.$refs.MyCustomer.openForm(row, '分配客户')
    },
    getDataList() {
      this.selectedRowKeys = []
      this.searchTipResultList = [] //清空搜索提示列表

      this.loading = true
      this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          this.loading = false
          this.data = resJson.Data
          this.getlist()
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    contactPerson(row) {
      this.$refs.List.openForm(row, '客户联系人')
    },
    // 市级change
    city(e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=' + e, {}).then((resJson) => {
        this.loading = false
        this.listChildren = resJson.Data
      })
    },

    // 县change
    village(e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=' + e, {}).then((resJson) => {
        this.loading = false

        this.county = resJson.Data
      })
    },
    handleEdit(id) {
      this.$refs.details.openForm(id, '客户详情')
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http.post('/CustomerManagement/Cust_CustomerInfoFormal/DeleteData', ids).then((resJson) => {
            resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
        })
      })
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    },
    async toGetSearchTipList(e) {
      this.searchTipResultList = []
      this.queryParam.cCusName = e
      if (!this.queryParam.cCusName) {
        return
      }
      await this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMoreCondition', {
          PageIndex: 1,
          PageRows: 10,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          if (resJson.Success && this.queryParam.cCusName) {
            this.searchTipResultList = resJson.Data.map((e) => e.cCusAbbName).slice(0, 10)
          } else {
            this.searchTipResultList = []
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    selectSearchTipFun(text) {
      this.queryParam.cCusName = text
      this.searchTipResultList = []
      this.getDataList()
    },

    keywordBlurFun() {
      this.searchTipResultList = []
    },
  },
}
</script>

<style lang="less" scoped>
/deep/ .ant-card-body {
  padding-bottom: 0px;
}
</style>
